extends PanelContainer

@onready var dawn_container = %dawn_container
@onready var morning_container = %morning_container
@onready var afternoon_container = %afternoon_container
@onready var evening_container = %evening_container

@onready var schedule_line_scene = preload("res://components/schedule_line/schedule_line.tscn")
@onready var h_seperator_scene = preload('res://components/h_separator.tscn')

@onready var containers := [morning_container, afternoon_container, evening_container, dawn_container]

# Called when the node enters the scene tree for the first time.
func _ready():
	Signalbus.date_selected.connect(_on_date_selected)
	Signalbus.date_selected.emit(GlobalVariables.get_current_date())
	

func clear_all_containers():
	for container in containers:
		clear_container(container)

func clear_container(container:Node):
	for child in container.get_children():
		container.remove_child(child)
		child.queue_free()

func add_schedule_line(container:Node, scheduleData:ScheduleData):
	
	if GlobalVariables.settingData.hideDelayedIterms and GlobalVariables.check_schedule_state(scheduleData) == GlobalVariables.ScheduleState.DELAYED:
		return
		
	if container.get_child_count()>0:
		container.add_child(h_seperator_scene.instantiate())
	
	var schedule_line = schedule_line_scene.instantiate()
	schedule_line.scheduleData = scheduleData
	container.add_child(schedule_line)

func _on_date_selected(date:Calendar.Date):
	clear_all_containers()
	var date_str = GlobalVariables.format_date(date)	
	# 获得当前各个时段的日程
	var period_schedules = GlobalVariables.date_period_schedules.get(date_str,{})
	# 遍历period
	for period in period_schedules.keys():
		var container = containers[period-1]
		for schedule_id in period_schedules[period]:
			var scheduleData = GlobalVariables.get_schedule_data(schedule_id)
			add_schedule_line(container,scheduleData)
		


